package com.fsxgt.datagrid.datax.writer.impl;

import static com.fsxgt.datagrid.datax.tool.DataXConstant.SPLIT_COLON;
import static com.fsxgt.datagrid.datax.tool.DataXConstant.STRING_BLANK;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.datax.plugin.rdbms.writer.Key;
import com.fsxgt.datagrid.component.bo.CmpSyncOfflineBO;
import com.fsxgt.datagrid.datax.writer.WriterBuilder;
import com.fsxgt.datagrid.ds.bo.DatasourceJdbcBO;
import com.fsxgt.datagrid.ds.component.QueryComponent;
import com.fsxgt.datagrid.ds.utils.ParamUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;

@Component
public abstract class RdbmsWriterBuilder  extends WriterBuilder {
	
	@Autowired
	private QueryComponent queryComponent;
	
	
    @Override
    public Map<String, Object> build(CmpSyncOfflineBO bo,Map params) throws Exception {
        Map<String, Object> writerObj = Maps.newLinkedHashMap();
        writerObj.put("name", getName());

        DatasourceJdbcBO targetDatasource = queryComponent.getDatasource(bo.getTargetDatasourceId());
        
        List targetColumns=new ArrayList();
		bo.getColumnList().forEach(b->{
			targetColumns.add(queryComponent.doConvertKeywordsColumn(targetDatasource.getDatasource(),b.getTargetColumnName()));
		});
		
        
        Map<String, Object> parameterObj = Maps.newLinkedHashMap();
//        parameterObj.put("writeMode", "insert");
    
        parameterObj.put("username", targetDatasource.getJdbcUsername());
        parameterObj.put("password", targetDatasource.getJdbcPassword());
        parameterObj.put("column", targetColumns);
        
        parameterObj.put(Key.BATCH_SIZE, bo.getBatchSize());
        parameterObj.put(Key.BATCH_BYTE_SIZE, bo.getBatchByteSize());
       
        if(bo.getTargetPreSql()!=null) {
        	parameterObj.put("preSql", splitSql(bo.getTargetPreSql()));
        }
        if(bo.getTargetPostSql()!=null) {
            parameterObj.put("postSql",splitSql(bo.getTargetPostSql()));
        }
        

        Map<String, Object> connectionObj = Maps.newLinkedHashMap();
        connectionObj.put("table", ImmutableList.of(bo.getTargetTableName())   );
        connectionObj.put("jdbcUrl", targetDatasource.getJdbcUrl());

        parameterObj.put("connection", ImmutableList.of(connectionObj));
        writerObj.put("parameter", parameterObj);

        return writerObj;
    }

    private String[] splitSql(String sql) {
        String[] sqlArr = null;
        if (StringUtils.isNotBlank(sql)) {
            Pattern p = Pattern.compile("\r\n|\r|\n|\n\r");
            Matcher m = p.matcher(sql);
            String sqlStr = m.replaceAll(STRING_BLANK);
            sqlArr = sqlStr.split(SPLIT_COLON);
        }
        return sqlArr;
    }


}
